using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using Unity.Mathematics;
using Unity.VisualScripting;
using Debug = UnityEngine.Debug;

namespace QFramework.Monk
{
	public class UINormalData : UIPanelData
	{
	}
	public partial class UINormal : UIPanel,IController
	{
		public static EasyEvent bombEvent = new EasyEvent();
		
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as UINormalData ?? new UINormalData();
			// please add init code here
			var configModel = this.GetModel<ConfigModel>();
			var expUpSystem = this.GetSystem<ExpUpSystem>();
			
			var nextLvExpConig = configModel.GetLevelUpConfig(AppStart.level.Value+1);
			if (nextLvExpConig==null)
			{
				nextLvExpConig = configModel.GetLevelUpConfig(AppStart.level.Value);
			}
			
			AppStart.level.RegisterWithInitValue((lv =>
			{
				if (!AppStart.CheckGameState(GameState.GamePlaying))
				{
					return;
				}
				Lv.text = "等级："+lv;
				if (configModel.CheckCanLevelUp(lv)&&lv!=0)
				{
					expUpSystem.RefeshExpUpItems();
					if (expUpSystem.expUpItems.Count>0)
					{
						LevelUpPanel.RefreshBtns();
						LevelUpPanel.Show();
						Time.timeScale = 0;
						nextLvExpConig = configModel.GetLevelUpConfig(lv+1);
						Exp.text = string.Format("经验值：{0}/{1}",0,nextLvExpConig.NeedExp);
						
					}
				}
			})).UnRegisterWhenGameObjectDestroyed(this.gameObject);
			
			AppStart.exp.RegisterWithInitValue((exp =>
			{
					Exp.text = string.Format("经验值：{0}/{1}",exp,nextLvExpConig.NeedExp);
					if (nextLvExpConig != null)
					{
						ExpBarValue.fillAmount = (float) exp / nextLvExpConig.NeedExp;
					}
					else
					{
						ExpBarValue.fillAmount = 1;
					}
			})).UnRegisterWhenGameObjectDestroyed(this.gameObject);
			
			
			AppStart.curHp.RegisterWithInitValue((num =>
			{
				if (num<0)
				{
					num = 0;//防止显示负值
				}
				HpText.text = string.Format("生命值：{0}/{1}",num,AppStart.maxHp.Value);
				
			})).UnRegisterWhenGameObjectDestroyed(this.gameObject);
			
			AppStart.curSec.RegisterWithInitValue((time =>
			{
				if (Time.frameCount%30==0)
				{
					var timeInt = Mathf.FloorToInt(time);
					var min = timeInt / 60;
					var sec = timeInt % 60;
					TimeText.text = string.Format("时间：{0:00}:{1:00}", min, sec);
				}
			})).UnRegisterWhenGameObjectDestroyed(this.gameObject);
			AppStart.enemyNum.RegisterWithInitValue(((num =>
			{
				EnemyNum.text = "敌人："+num;
				
			}))).UnRegisterWhenGameObjectDestroyed(this.gameObject);

			AppStart.goldNum.RegisterWithInitValue(((num =>
			{
				
				GoldNum.text = "金币："+num;
				
			}))).UnRegisterWhenGameObjectDestroyed(this.gameObject);
			
			ActionKit.OnUpdate.Register((() =>
			{
				if (!AppStart.CheckGameState(GameState.GamePlaying))
				{
					return;
				}
				AppStart.curSec.Value +=Time.deltaTime;
				
				
			} )).UnRegisterWhenGameObjectDestroyed((this.gameObject));

			bombEvent.Register(() =>
			{
				ActionKit.Sequence().Lerp(0, 1, 0.2f, (value) =>
				{
					ScreenColor.ColorAlpha(value);
				}).Lerp(1, 0, 0.2f, (value) =>
				{
					ScreenColor.ColorAlpha(value);
				}).Start(this);

			}).UnRegisterWhenGameObjectDestroyed(this.gameObject);

			expUpSystem.expUpEvent.Register(item =>
			{
				SkillPanel.RefshSkillItem(item);
				
			}).UnRegisterWhenGameObjectDestroyed(this.gameObject);
		}
		
		protected override void OnOpen(IUIData uiData = null)
		{
			LevelUpPanel.Hide();
			//SkillPanel.Show();
		}
		
		protected override void OnShow()
		{
		}
		
		protected override void OnHide()
		{
		}
		
		protected override void OnClose()
		{
		}

		public IArchitecture GetArchitecture()
		{
			return AppStart.Interface;
		}
	}
}
